
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" lang="zh_Hans">
  <head>
    <meta http-equiv="X-UA-Compatible" content="IE=Edge" />
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>站点地图框架 &#8212; Django 3.2.6.dev 文档</title>
    <link rel="stylesheet" href="../../_static/default.css" type="text/css" />
    <link rel="stylesheet" href="../../_static/pygments.css" type="text/css" />
    <script type="text/javascript" id="documentation_options" data-url_root="../../" src="../../_static/documentation_options.js"></script>
    <script type="text/javascript" src="../../_static/jquery.js"></script>
    <script type="text/javascript" src="../../_static/underscore.js"></script>
    <script type="text/javascript" src="../../_static/doctools.js"></script>
    <script type="text/javascript" src="../../_static/language_data.js"></script>
    <link rel="index" title="索引" href="../../genindex.html" />
    <link rel="search" title="搜索" href="../../search.html" />
    <link rel="next" title="“站点”框架" href="sites.html" />
    <link rel="prev" title="重定向应用" href="redirects.html" />



 
<script src="../../templatebuiltins.js"></script>
<script>
(function($) {
    if (!django_template_builtins) {
       // templatebuiltins.js missing, do nothing.
       return;
    }
    $(document).ready(function() {
        // Hyperlink Django template tags and filters
        var base = "../templates/builtins.html";
        if (base == "#") {
            // Special case for builtins.html itself
            base = "";
        }
        // Tags are keywords, class '.k'
        $("div.highlight\\-html\\+django span.k").each(function(i, elem) {
             var tagname = $(elem).text();
             if ($.inArray(tagname, django_template_builtins.ttags) != -1) {
                 var fragment = tagname.replace(/_/, '-');
                 $(elem).html("<a href='" + base + "#" + fragment + "'>" + tagname + "</a>");
             }
        });
        // Filters are functions, class '.nf'
        $("div.highlight\\-html\\+django span.nf").each(function(i, elem) {
             var filtername = $(elem).text();
             if ($.inArray(filtername, django_template_builtins.tfilters) != -1) {
                 var fragment = filtername.replace(/_/, '-');
                 $(elem).html("<a href='" + base + "#" + fragment + "'>" + filtername + "</a>");
             }
        });
    });
})(jQuery);</script>

  </head><body>

    <div class="document">
  <div id="custom-doc" class="yui-t6">
    <div id="hd">
      <h1><a href="../../index.html">Django 3.2.6.dev 文档</a></h1>
      <div id="global-nav">
        <a title="Home page" href="../../index.html">Home</a>  |
        <a title="Table of contents" href="../../contents.html">Table of contents</a>  |
        <a title="Global index" href="../../genindex.html">Index</a>  |
        <a title="Module index" href="../../py-modindex.html">Modules</a>
      </div>
      <div class="nav">
    &laquo; <a href="redirects.html" title="重定向应用">previous</a>
     |
    <a href="../index.html" title="API 参考" accesskey="U">up</a>
   |
    <a href="sites.html" title="“站点”框架">next</a> &raquo;</div>
    </div>

    <div id="bd">
      <div id="yui-main">
        <div class="yui-b">
          <div class="yui-g" id="ref-contrib-sitemaps">
            
  <div class="section" id="s-module-django.contrib.sitemaps">
<span id="s-the-sitemap-framework"></span><span id="module-django.contrib.sitemaps"></span><span id="the-sitemap-framework"></span><h1>站点地图框架<a class="headerlink" href="#module-django.contrib.sitemaps" title="永久链接至标题">¶</a></h1>
<p>Django 自带了一个高级站点地图生成框架来创建 <a class="reference external" href="https://www.sitemaps.org/">sitemap</a> XML 文件。</p>
<div class="section" id="s-overview">
<span id="overview"></span><h2>概况<a class="headerlink" href="#overview" title="永久链接至标题">¶</a></h2>
<p>站点地图是你网站上的一个 XML 文件，它告诉搜索引擎索引器你页面的变化频率，以及某些页面相对于你网站上其他页面的“重要性”。这些信息有助于搜索引擎对你的网站进行索引。</p>
<p>Django 站点地图框架通过让你用 Python 代码表达这些信息以自动创建这个 XML 文件。</p>
<p>它的工作原理很像 Django 的 <a class="reference internal" href="syndication.html"><span class="doc">联合框架</span></a>。要创建一个站点地图，写一个 <a class="reference internal" href="#django.contrib.sitemaps.Sitemap" title="django.contrib.sitemaps.Sitemap"><code class="xref py py-class docutils literal notranslate"><span class="pre">Sitemap</span></code></a> 类，并将其指向你的 <a class="reference internal" href="../../topics/http/urls.html"><span class="doc">URLconf</span></a>。</p>
</div>
<div class="section" id="s-installation">
<span id="installation"></span><h2>安装<a class="headerlink" href="#installation" title="永久链接至标题">¶</a></h2>
<p>要安装站点地图应用，请按照以下步骤进行：</p>
<ol class="arabic simple">
<li>将 <code class="docutils literal notranslate"><span class="pre">'django.contrib.sitemaps'</span></code> 添加到你的 <a class="reference internal" href="../settings.html#std:setting-INSTALLED_APPS"><code class="xref std std-setting docutils literal notranslate"><span class="pre">INSTALLED_APPS</span></code></a> 配置中。</li>
<li>确保你的 <a class="reference internal" href="../settings.html#std:setting-TEMPLATES"><code class="xref std std-setting docutils literal notranslate"><span class="pre">TEMPLATES</span></code></a> 配置中包含一个 <code class="docutils literal notranslate"><span class="pre">DjangoTemplates</span></code> 后端，其 <code class="docutils literal notranslate"><span class="pre">APP_DIRS</span></code> 选项设置为 <code class="docutils literal notranslate"><span class="pre">True</span></code>。默认有一个后端，所以你只需要改变这个配置就可以了。</li>
<li>确保你已经安装了 <a class="reference internal" href="sites.html#module-django.contrib.sites" title="django.contrib.sites: Lets you operate multiple websites from the same database and Django project"><code class="xref py py-mod docutils literal notranslate"><span class="pre">sites</span> <span class="pre">framework</span></code></a>。</li>
</ol>
<p>（注意：站点地图应用不会安装任何数据库表。它需要进入 <a class="reference internal" href="../settings.html#std:setting-INSTALLED_APPS"><code class="xref std std-setting docutils literal notranslate"><span class="pre">INSTALLED_APPS</span></code></a> 的唯一原因是为了让 <a class="reference internal" href="../templates/api.html#django.template.loaders.app_directories.Loader" title="django.template.loaders.app_directories.Loader"><code class="xref py py-func docutils literal notranslate"><span class="pre">Loader()</span></code></a> 模板加载器能够找到默认模板。）</p>
</div>
<div class="section" id="s-initialization">
<span id="initialization"></span><h2>初始化<a class="headerlink" href="#initialization" title="永久链接至标题">¶</a></h2>
<dl class="function">
<dt id="django.contrib.sitemaps.views.sitemap">
<code class="descclassname">views.</code><code class="descname">sitemap</code>(<em>request</em>, <em>sitemaps</em>, <em>section=None</em>, <em>template_name='sitemap.xml'</em>, <em>content_type='application/xml'</em>)<a class="headerlink" href="#django.contrib.sitemaps.views.sitemap" title="永久链接至目标">¶</a></dt>
<dd></dd></dl>

<p>要在你的 Django 网站上激活站点地图的生成，在你的 <a class="reference internal" href="../../topics/http/urls.html"><span class="doc">URLconf</span></a> 中添加这一行：</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">django.contrib.sitemaps.views</span> <span class="kn">import</span> <span class="n">sitemap</span>

<span class="n">path</span><span class="p">(</span><span class="s1">&#39;sitemap.xml&#39;</span><span class="p">,</span> <span class="n">sitemap</span><span class="p">,</span> <span class="p">{</span><span class="s1">&#39;sitemaps&#39;</span><span class="p">:</span> <span class="n">sitemaps</span><span class="p">},</span>
     <span class="n">name</span><span class="o">=</span><span class="s1">&#39;django.contrib.sitemaps.views.sitemap&#39;</span><span class="p">)</span>
</pre></div>
</div>
<p>这告诉 Django 在客户端访问 <code class="file docutils literal notranslate"><span class="pre">/sitemap.xml</span></code> 时建立一个站点地图。</p>
<p>站点地图文件的名称并不重要，但位置很重要。搜索引擎只对当前URL级别及以下的站点地图链接进行索引。例如，如果 <code class="file docutils literal notranslate"><span class="pre">sitemap.xml</span></code> 位于你的根目录下，它可以引用您网站中的任何URL。但是，如果您的站点地图位于 <code class="file docutils literal notranslate"><span class="pre">/content/sitemap.xml</span></code>，则只能引用以 <code class="file docutils literal notranslate"><span class="pre">/content/</span></code> 开头的 URL。</p>
<p>sitemap 视图需要一个额外的、必要的参数：<code class="docutils literal notranslate"><span class="pre">{'sitemaps':</span> <span class="pre">sitemaps}</span></code>。<code class="docutils literal notranslate"><span class="pre">sitemaps</span></code> 应该是一个字典，它将一个简短的章节标签（例如，<code class="docutils literal notranslate"><span class="pre">blog</span></code> 或 <code class="docutils literal notranslate"><span class="pre">news</span></code>）映射到它的 <a class="reference internal" href="#django.contrib.sitemaps.Sitemap" title="django.contrib.sitemaps.Sitemap"><code class="xref py py-class docutils literal notranslate"><span class="pre">Sitemap</span></code></a> 类（例如，<code class="docutils literal notranslate"><span class="pre">BlogSitemap</span></code> 或 <code class="docutils literal notranslate"><span class="pre">NewsSitemap</span></code>）。也可以映射到一个 <a class="reference internal" href="#django.contrib.sitemaps.Sitemap" title="django.contrib.sitemaps.Sitemap"><code class="xref py py-class docutils literal notranslate"><span class="pre">Sitemap</span></code></a> 类的 <em>实例</em> （例如，<code class="docutils literal notranslate"><span class="pre">BlogSitemap(some_var)</span></code>）。</p>
</div>
<div class="section" id="s-sitemap-classes">
<span id="sitemap-classes"></span><h2><code class="docutils literal notranslate"><span class="pre">Sitemap</span></code> 类<a class="headerlink" href="#sitemap-classes" title="永久链接至标题">¶</a></h2>
<p><a class="reference internal" href="#django.contrib.sitemaps.Sitemap" title="django.contrib.sitemaps.Sitemap"><code class="xref py py-class docutils literal notranslate"><span class="pre">Sitemap</span></code></a> 类是一个 Python 类，它代表了站点地图中的“部分”条目。例如，一个 <a class="reference internal" href="#django.contrib.sitemaps.Sitemap" title="django.contrib.sitemaps.Sitemap"><code class="xref py py-class docutils literal notranslate"><span class="pre">Sitemap</span></code></a> 类可以代表你的 Weblog 的所有条目，而另一个可以代表你的事件日历中的所有事件。</p>
<p>在最简单的情况下，所有这些部分都被归纳到一个 <code class="file docutils literal notranslate"><span class="pre">sitemap.xml</span></code> 中，但也可以使用该框架生成一个站点地图索引来引用单个站点地图文件，每个部分一个。（参见下面的 <a class="reference internal" href="#creating-a-sitemap-index">创建一个站点地图索引</a> ）。</p>
<p><a class="reference internal" href="#django.contrib.sitemaps.Sitemap" title="django.contrib.sitemaps.Sitemap"><code class="xref py py-class docutils literal notranslate"><span class="pre">Sitemap</span></code></a> 类必须是 <code class="docutils literal notranslate"><span class="pre">django.contrib.sitemaps.Sitemap</span></code> 子类。它们可以存在于你代码库的任何地方。</p>
</div>
<div class="section" id="s-an-example">
<span id="an-example"></span><h2>一个例子<a class="headerlink" href="#an-example" title="永久链接至标题">¶</a></h2>
<p>让我们假设你有一个博客系统，有一个 <code class="docutils literal notranslate"><span class="pre">Entry</span></code> 模型，你希望你的网站地图包括所有链接到你的个人博客条目。下面是你的网站地图类的样子：</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">django.contrib.sitemaps</span> <span class="kn">import</span> <span class="n">Sitemap</span>
<span class="kn">from</span> <span class="nn">blog.models</span> <span class="kn">import</span> <span class="n">Entry</span>

<span class="k">class</span> <span class="nc">BlogSitemap</span><span class="p">(</span><span class="n">Sitemap</span><span class="p">):</span>
    <span class="n">changefreq</span> <span class="o">=</span> <span class="s2">&quot;never&quot;</span>
    <span class="n">priority</span> <span class="o">=</span> <span class="mf">0.5</span>

    <span class="k">def</span> <span class="nf">items</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">return</span> <span class="n">Entry</span><span class="o">.</span><span class="n">objects</span><span class="o">.</span><span class="n">filter</span><span class="p">(</span><span class="n">is_draft</span><span class="o">=</span><span class="kc">False</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">lastmod</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">obj</span><span class="p">):</span>
        <span class="k">return</span> <span class="n">obj</span><span class="o">.</span><span class="n">pub_date</span>
</pre></div>
</div>
<p>注意：</p>
<ul class="simple">
<li><a class="reference internal" href="#django.contrib.sitemaps.Sitemap.changefreq" title="django.contrib.sitemaps.Sitemap.changefreq"><code class="xref py py-attr docutils literal notranslate"><span class="pre">changefreq</span></code></a> 和 <a class="reference internal" href="#django.contrib.sitemaps.Sitemap.priority" title="django.contrib.sitemaps.Sitemap.priority"><code class="xref py py-attr docutils literal notranslate"><span class="pre">priority</span></code></a> 分别是用作 <code class="docutils literal notranslate"><span class="pre">&lt;changefreq&gt;</span></code> 和 <code class="docutils literal notranslate"><span class="pre">&lt;priority&gt;</span></code> 元素的类属性。它们可以作为函数调用，就像本例中的 <a class="reference internal" href="#django.contrib.sitemaps.Sitemap.lastmod" title="django.contrib.sitemaps.Sitemap.lastmod"><code class="xref py py-attr docutils literal notranslate"><span class="pre">lastmod</span></code></a> 一样。</li>
<li><a class="reference internal" href="#django.contrib.sitemaps.Sitemap.items" title="django.contrib.sitemaps.Sitemap.items"><code class="xref py py-attr docutils literal notranslate"><span class="pre">items()</span></code></a> 是一个返回 <a class="reference external" href="https://docs.python.org/3/glossary.html#term-sequence" title="(在 Python v3.9)"><span class="xref std std-term">sequence</span></a> 或 <code class="docutils literal notranslate"><span class="pre">QuerySet</span></code> 对象的方法。返回的对象将被传递给对应于站点地图属性的任何可调用方法（<a class="reference internal" href="#django.contrib.sitemaps.Sitemap.location" title="django.contrib.sitemaps.Sitemap.location"><code class="xref py py-attr docutils literal notranslate"><span class="pre">location</span></code></a>，<a class="reference internal" href="#django.contrib.sitemaps.Sitemap.lastmod" title="django.contrib.sitemaps.Sitemap.lastmod"><code class="xref py py-attr docutils literal notranslate"><span class="pre">lastmod</span></code></a>，<a class="reference internal" href="#django.contrib.sitemaps.Sitemap.changefreq" title="django.contrib.sitemaps.Sitemap.changefreq"><code class="xref py py-attr docutils literal notranslate"><span class="pre">changefreq</span></code></a> 和 <a class="reference internal" href="#django.contrib.sitemaps.Sitemap.priority" title="django.contrib.sitemaps.Sitemap.priority"><code class="xref py py-attr docutils literal notranslate"><span class="pre">priority</span></code></a>）。</li>
<li><a class="reference internal" href="#django.contrib.sitemaps.Sitemap.lastmod" title="django.contrib.sitemaps.Sitemap.lastmod"><code class="xref py py-attr docutils literal notranslate"><span class="pre">lastmod</span></code></a> 应该返回一个 <a class="reference external" href="https://docs.python.org/3/library/datetime.html#datetime.datetime" title="(在 Python v3.9)"><code class="xref py py-class docutils literal notranslate"><span class="pre">datetime</span></code></a>。</li>
<li>本例中没有 <a class="reference internal" href="#django.contrib.sitemaps.Sitemap.location" title="django.contrib.sitemaps.Sitemap.location"><code class="xref py py-attr docutils literal notranslate"><span class="pre">location</span></code></a> 方法，但你可以提供该方法来指定对象的 URL。默认情况下，<a class="reference internal" href="#django.contrib.sitemaps.Sitemap.location" title="django.contrib.sitemaps.Sitemap.location"><code class="xref py py-attr docutils literal notranslate"><span class="pre">location()</span></code></a> 调用每个对象的 <code class="docutils literal notranslate"><span class="pre">get_absolute_url()</span></code> 并返回结果。</li>
</ul>
</div>
<div class="section" id="s-sitemap-class-reference">
<span id="sitemap-class-reference"></span><h2><code class="docutils literal notranslate"><span class="pre">Sitemap</span></code> 类参考<a class="headerlink" href="#sitemap-class-reference" title="永久链接至标题">¶</a></h2>
<dl class="class">
<dt id="django.contrib.sitemaps.Sitemap">
<em class="property">class </em><code class="descname">Sitemap</code><a class="headerlink" href="#django.contrib.sitemaps.Sitemap" title="永久链接至目标">¶</a></dt>
<dd><p><code class="docutils literal notranslate"><span class="pre">Sitemap</span></code> 类可以定义以下方法／属性。</p>
<dl class="attribute">
<dt id="django.contrib.sitemaps.Sitemap.items">
<code class="descname">items</code><a class="headerlink" href="#django.contrib.sitemaps.Sitemap.items" title="永久链接至目标">¶</a></dt>
<dd><p><strong>必须的。</strong> 一个返回对象的 <a class="reference external" href="https://docs.python.org/3/glossary.html#term-sequence" title="(在 Python v3.9)"><span class="xref std std-term">sequence</span></a> 或 <code class="docutils literal notranslate"><span class="pre">QuerySet</span></code> 的方法。框架并不关心它们是什么类型的对象，重要的是这些对象被传递给 <a class="reference internal" href="#django.contrib.sitemaps.Sitemap.location" title="django.contrib.sitemaps.Sitemap.location"><code class="xref py py-attr docutils literal notranslate"><span class="pre">location()</span></code></a>、<a class="reference internal" href="#django.contrib.sitemaps.Sitemap.lastmod" title="django.contrib.sitemaps.Sitemap.lastmod"><code class="xref py py-attr docutils literal notranslate"><span class="pre">lastmod()</span></code></a>、<a class="reference internal" href="#django.contrib.sitemaps.Sitemap.changefreq" title="django.contrib.sitemaps.Sitemap.changefreq"><code class="xref py py-attr docutils literal notranslate"><span class="pre">changefreq()</span></code></a> 和 <a class="reference internal" href="#django.contrib.sitemaps.Sitemap.priority" title="django.contrib.sitemaps.Sitemap.priority"><code class="xref py py-attr docutils literal notranslate"><span class="pre">priority()</span></code></a> 方法。</p>
</dd></dl>

<dl class="attribute">
<dt id="django.contrib.sitemaps.Sitemap.location">
<code class="descname">location</code><a class="headerlink" href="#django.contrib.sitemaps.Sitemap.location" title="永久链接至目标">¶</a></dt>
<dd><p><strong>可选的。</strong> 方法或属性。</p>
<p>如果是方法，则应返回 <a class="reference internal" href="#django.contrib.sitemaps.Sitemap.items" title="django.contrib.sitemaps.Sitemap.items"><code class="xref py py-attr docutils literal notranslate"><span class="pre">items()</span></code></a> 返回的给定对象的绝对路径。</p>
<p>如果它是一个属性，它的值应该是一个字符串，代表一个绝对路径，用于 <a class="reference internal" href="#django.contrib.sitemaps.Sitemap.items" title="django.contrib.sitemaps.Sitemap.items"><code class="xref py py-attr docutils literal notranslate"><span class="pre">items()</span></code></a> 返回的 <em>每一个</em> 对象。</p>
<p>在这两种情况下，“绝对路径”指的是不包含协议或域名的 URL。例如：</p>
<ul class="simple">
<li>对的：<code class="file docutils literal notranslate"><span class="pre">'/foo/bar/'</span></code></li>
<li>错的：<code class="file docutils literal notranslate"><span class="pre">'example.com/foo/bar/'</span></code></li>
<li>错的：<code class="file docutils literal notranslate"><span class="pre">'https://example.com/foo/bar/'</span></code></li>
</ul>
<p>如果没有提供 <a class="reference internal" href="#django.contrib.sitemaps.Sitemap.location" title="django.contrib.sitemaps.Sitemap.location"><code class="xref py py-attr docutils literal notranslate"><span class="pre">location</span></code></a>，框架将调用 <a class="reference internal" href="#django.contrib.sitemaps.Sitemap.items" title="django.contrib.sitemaps.Sitemap.items"><code class="xref py py-attr docutils literal notranslate"><span class="pre">items()</span></code></a> 返回的每个对象的 <code class="docutils literal notranslate"><span class="pre">get_absolute_url()</span></code> 方法。</p>
<p>如果要指定 <code class="docutils literal notranslate"><span class="pre">'http'</span></code> 以外的协议，请使用 <a class="reference internal" href="#django.contrib.sitemaps.Sitemap.protocol" title="django.contrib.sitemaps.Sitemap.protocol"><code class="xref py py-attr docutils literal notranslate"><span class="pre">protocol</span></code></a>。</p>
</dd></dl>

<dl class="attribute">
<dt id="django.contrib.sitemaps.Sitemap.lastmod">
<code class="descname">lastmod</code><a class="headerlink" href="#django.contrib.sitemaps.Sitemap.lastmod" title="永久链接至目标">¶</a></dt>
<dd><p><strong>可选的。</strong> 方法或属性。</p>
<p>如果它是一个方法，它应该接受一个参数——一个由 <a class="reference internal" href="#django.contrib.sitemaps.Sitemap.items" title="django.contrib.sitemaps.Sitemap.items"><code class="xref py py-attr docutils literal notranslate"><span class="pre">items()</span></code></a> 返回的对象——并以 <a class="reference external" href="https://docs.python.org/3/library/datetime.html#datetime.datetime" title="(在 Python v3.9)"><code class="xref py py-class docutils literal notranslate"><span class="pre">datetime</span></code></a> 返回该对象最后修改的日期／时间。</p>
<p>如果是属性，其值应该是 <a class="reference external" href="https://docs.python.org/3/library/datetime.html#datetime.datetime" title="(在 Python v3.9)"><code class="xref py py-class docutils literal notranslate"><span class="pre">datetime</span></code></a>，代表 <a class="reference internal" href="#django.contrib.sitemaps.Sitemap.items" title="django.contrib.sitemaps.Sitemap.items"><code class="xref py py-attr docutils literal notranslate"><span class="pre">items()</span></code></a> 返回的 <em>每一个</em> 对象的最后修改日期／时间。</p>
<p>如果站点地图中的所有项目都有一个 <a class="reference internal" href="#django.contrib.sitemaps.Sitemap.lastmod" title="django.contrib.sitemaps.Sitemap.lastmod"><code class="xref py py-attr docutils literal notranslate"><span class="pre">lastmod</span></code></a>，那么由 <a class="reference internal" href="#django.contrib.sitemaps.views.sitemap" title="django.contrib.sitemaps.views.sitemap"><code class="xref py py-func docutils literal notranslate"><span class="pre">views.sitemap()</span></code></a> 生成的站点地图将有一个 <code class="docutils literal notranslate"><span class="pre">Last-Modified</span></code> 头，等于最新的 <code class="docutils literal notranslate"><span class="pre">lastmod</span></code>。你可以激活 <a class="reference internal" href="../middleware.html#django.middleware.http.ConditionalGetMiddleware" title="django.middleware.http.ConditionalGetMiddleware"><code class="xref py py-class docutils literal notranslate"><span class="pre">ConditionalGetMiddleware</span></code></a>，让 Django 对请求做出适当的响应，并提供 <code class="docutils literal notranslate"><span class="pre">If-Modified-Since</span></code> 头，防止发送没有变化的站点地图。</p>
</dd></dl>

<dl class="attribute">
<dt id="django.contrib.sitemaps.Sitemap.paginator">
<code class="descname">paginator</code><a class="headerlink" href="#django.contrib.sitemaps.Sitemap.paginator" title="永久链接至目标">¶</a></dt>
<dd><p><strong>可选的。</strong></p>
<p>This property returns a <a class="reference internal" href="../paginator.html#django.core.paginator.Paginator" title="django.core.paginator.Paginator"><code class="xref py py-class docutils literal notranslate"><span class="pre">Paginator</span></code></a> for
<a class="reference internal" href="#django.contrib.sitemaps.Sitemap.items" title="django.contrib.sitemaps.Sitemap.items"><code class="xref py py-attr docutils literal notranslate"><span class="pre">items()</span></code></a>. If you generate sitemaps in a batch you may
want to override this as a cached property in order to avoid multiple
<code class="docutils literal notranslate"><span class="pre">items()</span></code> calls.</p>
</dd></dl>

<dl class="attribute">
<dt id="django.contrib.sitemaps.Sitemap.changefreq">
<code class="descname">changefreq</code><a class="headerlink" href="#django.contrib.sitemaps.Sitemap.changefreq" title="永久链接至目标">¶</a></dt>
<dd><p><strong>可选的。</strong> 方法或属性。</p>
<p>如果它是一个方法，它应该接受一个参数——一个由 <a class="reference internal" href="#django.contrib.sitemaps.Sitemap.items" title="django.contrib.sitemaps.Sitemap.items"><code class="xref py py-attr docutils literal notranslate"><span class="pre">items()</span></code></a> 返回的对象——并以字符串形式返回该对象的变化频率。</p>
<p>如果是属性，其值应该是一个字符串，代表 <a class="reference internal" href="#django.contrib.sitemaps.Sitemap.items" title="django.contrib.sitemaps.Sitemap.items"><code class="xref py py-attr docutils literal notranslate"><span class="pre">items()</span></code></a> 返回的 <em>每一个</em> 对象的变化频率。</p>
<p><a class="reference internal" href="#django.contrib.sitemaps.Sitemap.changefreq" title="django.contrib.sitemaps.Sitemap.changefreq"><code class="xref py py-attr docutils literal notranslate"><span class="pre">changefreq</span></code></a> 的可能值，无论你是使用方法还是属性，都是：</p>
<ul class="simple">
<li><code class="docutils literal notranslate"><span class="pre">'always'</span></code></li>
<li><code class="docutils literal notranslate"><span class="pre">'hourly'</span></code></li>
<li><code class="docutils literal notranslate"><span class="pre">'daily'</span></code></li>
<li><code class="docutils literal notranslate"><span class="pre">'weekly'</span></code></li>
<li><code class="docutils literal notranslate"><span class="pre">'monthly'</span></code></li>
<li><code class="docutils literal notranslate"><span class="pre">'yearly'</span></code></li>
<li><code class="docutils literal notranslate"><span class="pre">'never'</span></code></li>
</ul>
</dd></dl>

<dl class="attribute">
<dt id="django.contrib.sitemaps.Sitemap.priority">
<code class="descname">priority</code><a class="headerlink" href="#django.contrib.sitemaps.Sitemap.priority" title="永久链接至目标">¶</a></dt>
<dd><p><strong>可选的。</strong> 方法或属性。</p>
<p>如果它是一个方法，它应该接受一个参数——一个由 <a class="reference internal" href="#django.contrib.sitemaps.Sitemap.items" title="django.contrib.sitemaps.Sitemap.items"><code class="xref py py-attr docutils literal notranslate"><span class="pre">items()</span></code></a> 返回的对象——并以字符串或浮点数的形式返回该对象的优先级。</p>
<p>如果它是一个属性，它的值应该是一个字符串或浮点数，代表 <a class="reference internal" href="#django.contrib.sitemaps.Sitemap.items" title="django.contrib.sitemaps.Sitemap.items"><code class="xref py py-attr docutils literal notranslate"><span class="pre">items()</span></code></a> 返回的 <em>每一个</em> 对象的优先级。</p>
<p><a class="reference internal" href="#django.contrib.sitemaps.Sitemap.priority" title="django.contrib.sitemaps.Sitemap.priority"><code class="xref py py-attr docutils literal notranslate"><span class="pre">priority</span></code></a> 的示例值：<code class="docutils literal notranslate"><span class="pre">0.4</span></code>，<code class="docutils literal notranslate"><span class="pre">1.0</span></code>。页面的默认优先级是 <code class="docutils literal notranslate"><span class="pre">0.5</span></code>。更多信息请参见  <a class="reference external" href="https://www.sitemaps.org/protocol.html#prioritydef">sitemaps.org documentation</a> 。</p>
</dd></dl>

<dl class="attribute">
<dt id="django.contrib.sitemaps.Sitemap.protocol">
<code class="descname">protocol</code><a class="headerlink" href="#django.contrib.sitemaps.Sitemap.protocol" title="永久链接至目标">¶</a></dt>
<dd><p><strong>可选的。</strong></p>
<p>该属性定义了网站地图中 URL 的协议（<code class="docutils literal notranslate"><span class="pre">'http'</span></code> 或 <code class="docutils literal notranslate"><span class="pre">'https'</span></code>）。如果没有设置，则使用请求网站地图的协议。如果站点地图是在请求之外建立的，则默认为 <code class="docutils literal notranslate"><span class="pre">'http'</span></code>。</p>
</dd></dl>

<dl class="attribute">
<dt id="django.contrib.sitemaps.Sitemap.limit">
<code class="descname">limit</code><a class="headerlink" href="#django.contrib.sitemaps.Sitemap.limit" title="永久链接至目标">¶</a></dt>
<dd><p><strong>可选的。</strong></p>
<p>该属性定义了网站地图每一页所包含的 URL 的最大数量。其值不应超过默认值 <code class="docutils literal notranslate"><span class="pre">50000</span></code>，这是 <a class="reference external" href="https://www.sitemaps.org/protocol.html#index">Sitemaps protocol</a> 中允许的上限。</p>
</dd></dl>

<dl class="attribute">
<dt id="django.contrib.sitemaps.Sitemap.i18n">
<code class="descname">i18n</code><a class="headerlink" href="#django.contrib.sitemaps.Sitemap.i18n" title="永久链接至目标">¶</a></dt>
<dd><p><strong>可选的。</strong></p>
<p>一个布尔属性，用于定义该网站地图的 URL 是否应使用所有的 <a class="reference internal" href="../settings.html#std:setting-LANGUAGES"><code class="xref std std-setting docutils literal notranslate"><span class="pre">LANGUAGES</span></code></a> 生成。默认值是 <code class="docutils literal notranslate"><span class="pre">False</span></code>。</p>
</dd></dl>

<dl class="attribute">
<dt id="django.contrib.sitemaps.Sitemap.languages">
<code class="descname">languages</code><a class="headerlink" href="#django.contrib.sitemaps.Sitemap.languages" title="永久链接至目标">¶</a></dt>
<dd><div class="versionadded">
<span class="title">New in Django 3.2.</span> </div>
<p><strong>可选的。</strong></p>
<p>启用 <a class="reference internal" href="#django.contrib.sitemaps.Sitemap.i18n" title="django.contrib.sitemaps.Sitemap.i18n"><code class="xref py py-attr docutils literal notranslate"><span class="pre">i18n</span></code></a> 时用于生成备用链接的 <a class="reference internal" href="../../topics/i18n/index.html#term-language-code"><span class="xref std std-term">语言代码</span></a> <a class="reference external" href="https://docs.python.org/3/glossary.html#term-sequence" title="(在 Python v3.9)"><span class="xref std std-term">sequence</span></a>。默认为 <a class="reference internal" href="../settings.html#std:setting-LANGUAGES"><code class="xref std std-setting docutils literal notranslate"><span class="pre">LANGUAGES</span></code></a>。</p>
</dd></dl>

<dl class="attribute">
<dt id="django.contrib.sitemaps.Sitemap.alternates">
<code class="descname">alternates</code><a class="headerlink" href="#django.contrib.sitemaps.Sitemap.alternates" title="永久链接至目标">¶</a></dt>
<dd><div class="versionadded">
<span class="title">New in Django 3.2.</span> </div>
<p><strong>可选的。</strong></p>
<p>一个布尔属性。当与 <a class="reference internal" href="#django.contrib.sitemaps.Sitemap.i18n" title="django.contrib.sitemaps.Sitemap.i18n"><code class="xref py py-attr docutils literal notranslate"><span class="pre">i18n</span></code></a> 结合使用时，生成的 URL 将有一个备用链接列表，使用 <a class="reference external" href="https://developers.google.com/search/docs/advanced/crawling/localized-versions">hreflang attribute</a> 指向其他语言版本。默认为 <code class="docutils literal notranslate"><span class="pre">False</span></code>。</p>
</dd></dl>

<dl class="attribute">
<dt id="django.contrib.sitemaps.Sitemap.x_default">
<code class="descname">x_default</code><a class="headerlink" href="#django.contrib.sitemaps.Sitemap.x_default" title="永久链接至目标">¶</a></dt>
<dd><div class="versionadded">
<span class="title">New in Django 3.2.</span> </div>
<p><strong>可选的。</strong></p>
<p>一个布尔属性。当 <code class="docutils literal notranslate"><span class="pre">True</span></code> 时，由 <a class="reference internal" href="#django.contrib.sitemaps.Sitemap.alternates" title="django.contrib.sitemaps.Sitemap.alternates"><code class="xref py py-attr docutils literal notranslate"><span class="pre">alternates</span></code></a> 生成的备用链接将包含一个 <code class="docutils literal notranslate"><span class="pre">hreflang=&quot;x-default&quot;</span></code> 回退条目，其值为 <a class="reference internal" href="../settings.html#std:setting-LANGUAGE_CODE"><code class="xref std std-setting docutils literal notranslate"><span class="pre">LANGUAGE_CODE</span></code></a>。默认值是 <code class="docutils literal notranslate"><span class="pre">False</span></code>。</p>
</dd></dl>

</dd></dl>

</div>
<div class="section" id="s-shortcuts">
<span id="shortcuts"></span><h2>捷径<a class="headerlink" href="#shortcuts" title="永久链接至标题">¶</a></h2>
<p>站点地图框架为常见的情况提供了一个方便的类：</p>
<dl class="class">
<dt id="django.contrib.sitemaps.GenericSitemap">
<em class="property">class </em><code class="descname">GenericSitemap</code>(<em>info_dict</em>, <em>priority=None</em>, <em>changefreq=None</em>, <em>protocol=None</em>)<a class="headerlink" href="#django.contrib.sitemaps.GenericSitemap" title="永久链接至目标">¶</a></dt>
<dd><p><a class="reference internal" href="#django.contrib.sitemaps.GenericSitemap" title="django.contrib.sitemaps.GenericSitemap"><code class="xref py py-class docutils literal notranslate"><span class="pre">django.contrib.sitemaps.GenericSitemap</span></code></a> 类允许你通过传递一个至少包含一个 <code class="docutils literal notranslate"><span class="pre">queryset</span></code> 条目的字典来创建一个站点地图。该查询结果集将用于生成站点地图的项目。它也可以有一个 <code class="docutils literal notranslate"><span class="pre">date_field</span></code> 条目，为从 <code class="docutils literal notranslate"><span class="pre">queryset</span></code> 中检索的对象指定一个日期字段。这将用于生成的站点地图中的 <a class="reference internal" href="#django.contrib.sitemaps.Sitemap.lastmod" title="django.contrib.sitemaps.Sitemap.lastmod"><code class="xref py py-attr docutils literal notranslate"><span class="pre">lastmod</span></code></a> 属性。</p>
<p><a class="reference internal" href="#django.contrib.sitemaps.Sitemap.priority" title="django.contrib.sitemaps.Sitemap.priority"><code class="xref py py-attr docutils literal notranslate"><span class="pre">priority</span></code></a>、<a class="reference internal" href="#django.contrib.sitemaps.Sitemap.changefreq" title="django.contrib.sitemaps.Sitemap.changefreq"><code class="xref py py-attr docutils literal notranslate"><span class="pre">changefreq</span></code></a> 和 <a class="reference internal" href="#django.contrib.sitemaps.Sitemap.protocol" title="django.contrib.sitemaps.Sitemap.protocol"><code class="xref py py-attr docutils literal notranslate"><span class="pre">protocol</span></code></a> 关键字参数允许为所有 URL 指定这些属性。</p>
</dd></dl>

<div class="section" id="s-example">
<span id="example"></span><h3>例如<a class="headerlink" href="#example" title="永久链接至标题">¶</a></h3>
<p>下面是一个使用 <a class="reference internal" href="#django.contrib.sitemaps.GenericSitemap" title="django.contrib.sitemaps.GenericSitemap"><code class="xref py py-class docutils literal notranslate"><span class="pre">GenericSitemap</span></code></a> 的 <a class="reference internal" href="../../topics/http/urls.html"><span class="doc">URLconf</span></a> 例子：</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">django.contrib.sitemaps</span> <span class="kn">import</span> <span class="n">GenericSitemap</span>
<span class="kn">from</span> <span class="nn">django.contrib.sitemaps.views</span> <span class="kn">import</span> <span class="n">sitemap</span>
<span class="kn">from</span> <span class="nn">django.urls</span> <span class="kn">import</span> <span class="n">path</span>
<span class="kn">from</span> <span class="nn">blog.models</span> <span class="kn">import</span> <span class="n">Entry</span>

<span class="n">info_dict</span> <span class="o">=</span> <span class="p">{</span>
    <span class="s1">&#39;queryset&#39;</span><span class="p">:</span> <span class="n">Entry</span><span class="o">.</span><span class="n">objects</span><span class="o">.</span><span class="n">all</span><span class="p">(),</span>
    <span class="s1">&#39;date_field&#39;</span><span class="p">:</span> <span class="s1">&#39;pub_date&#39;</span><span class="p">,</span>
<span class="p">}</span>

<span class="n">urlpatterns</span> <span class="o">=</span> <span class="p">[</span>
    <span class="c1"># some generic view using info_dict</span>
    <span class="c1"># ...</span>

    <span class="c1"># the sitemap</span>
    <span class="n">path</span><span class="p">(</span><span class="s1">&#39;sitemap.xml&#39;</span><span class="p">,</span> <span class="n">sitemap</span><span class="p">,</span>
         <span class="p">{</span><span class="s1">&#39;sitemaps&#39;</span><span class="p">:</span> <span class="p">{</span><span class="s1">&#39;blog&#39;</span><span class="p">:</span> <span class="n">GenericSitemap</span><span class="p">(</span><span class="n">info_dict</span><span class="p">,</span> <span class="n">priority</span><span class="o">=</span><span class="mf">0.6</span><span class="p">)}},</span>
         <span class="n">name</span><span class="o">=</span><span class="s1">&#39;django.contrib.sitemaps.views.sitemap&#39;</span><span class="p">),</span>
<span class="p">]</span>
</pre></div>
</div>
</div>
</div>
<div class="section" id="s-sitemap-for-static-views">
<span id="sitemap-for-static-views"></span><h2>静态视图的站点地图<a class="headerlink" href="#sitemap-for-static-views" title="永久链接至标题">¶</a></h2>
<p>通常情况下，你希望搜索引擎爬虫能够索引那些既不是对象详情页也不是简单页面的视图。解决的办法是在 <code class="docutils literal notranslate"><span class="pre">items</span></code> 中明确列出这些视图的 URL 名称，并在站点地图的 <code class="docutils literal notranslate"><span class="pre">location</span></code> 方法中调用 <a class="reference internal" href="../urlresolvers.html#django.urls.reverse" title="django.urls.reverse"><code class="xref py py-func docutils literal notranslate"><span class="pre">reverse()</span></code></a>。例如：</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="c1"># sitemaps.py</span>
<span class="kn">from</span> <span class="nn">django.contrib</span> <span class="kn">import</span> <span class="n">sitemaps</span>
<span class="kn">from</span> <span class="nn">django.urls</span> <span class="kn">import</span> <span class="n">reverse</span>

<span class="k">class</span> <span class="nc">StaticViewSitemap</span><span class="p">(</span><span class="n">sitemaps</span><span class="o">.</span><span class="n">Sitemap</span><span class="p">):</span>
    <span class="n">priority</span> <span class="o">=</span> <span class="mf">0.5</span>
    <span class="n">changefreq</span> <span class="o">=</span> <span class="s1">&#39;daily&#39;</span>

    <span class="k">def</span> <span class="nf">items</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">return</span> <span class="p">[</span><span class="s1">&#39;main&#39;</span><span class="p">,</span> <span class="s1">&#39;about&#39;</span><span class="p">,</span> <span class="s1">&#39;license&#39;</span><span class="p">]</span>

    <span class="k">def</span> <span class="nf">location</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">item</span><span class="p">):</span>
        <span class="k">return</span> <span class="n">reverse</span><span class="p">(</span><span class="n">item</span><span class="p">)</span>

<span class="c1"># urls.py</span>
<span class="kn">from</span> <span class="nn">django.contrib.sitemaps.views</span> <span class="kn">import</span> <span class="n">sitemap</span>
<span class="kn">from</span> <span class="nn">django.urls</span> <span class="kn">import</span> <span class="n">path</span>

<span class="kn">from</span> <span class="nn">.sitemaps</span> <span class="kn">import</span> <span class="n">StaticViewSitemap</span>
<span class="kn">from</span> <span class="nn">.</span> <span class="kn">import</span> <span class="n">views</span>

<span class="n">sitemaps</span> <span class="o">=</span> <span class="p">{</span>
    <span class="s1">&#39;static&#39;</span><span class="p">:</span> <span class="n">StaticViewSitemap</span><span class="p">,</span>
<span class="p">}</span>

<span class="n">urlpatterns</span> <span class="o">=</span> <span class="p">[</span>
    <span class="n">path</span><span class="p">(</span><span class="s1">&#39;&#39;</span><span class="p">,</span> <span class="n">views</span><span class="o">.</span><span class="n">main</span><span class="p">,</span> <span class="n">name</span><span class="o">=</span><span class="s1">&#39;main&#39;</span><span class="p">),</span>
    <span class="n">path</span><span class="p">(</span><span class="s1">&#39;about/&#39;</span><span class="p">,</span> <span class="n">views</span><span class="o">.</span><span class="n">about</span><span class="p">,</span> <span class="n">name</span><span class="o">=</span><span class="s1">&#39;about&#39;</span><span class="p">),</span>
    <span class="n">path</span><span class="p">(</span><span class="s1">&#39;license/&#39;</span><span class="p">,</span> <span class="n">views</span><span class="o">.</span><span class="n">license</span><span class="p">,</span> <span class="n">name</span><span class="o">=</span><span class="s1">&#39;license&#39;</span><span class="p">),</span>
    <span class="c1"># ...</span>
    <span class="n">path</span><span class="p">(</span><span class="s1">&#39;sitemap.xml&#39;</span><span class="p">,</span> <span class="n">sitemap</span><span class="p">,</span> <span class="p">{</span><span class="s1">&#39;sitemaps&#39;</span><span class="p">:</span> <span class="n">sitemaps</span><span class="p">},</span>
         <span class="n">name</span><span class="o">=</span><span class="s1">&#39;django.contrib.sitemaps.views.sitemap&#39;</span><span class="p">)</span>
<span class="p">]</span>
</pre></div>
</div>
</div>
<div class="section" id="s-creating-a-sitemap-index">
<span id="creating-a-sitemap-index"></span><h2>创建站点地图索引<a class="headerlink" href="#creating-a-sitemap-index" title="永久链接至标题">¶</a></h2>
<dl class="function">
<dt id="django.contrib.sitemaps.views.index">
<code class="descclassname">views.</code><code class="descname">index</code>(<em>request</em>, <em>sitemaps</em>, <em>template_name='sitemap_index.xml'</em>, <em>content_type='application/xml'</em>, <em>sitemap_url_name='django.contrib.sitemaps.views.sitemap'</em>)<a class="headerlink" href="#django.contrib.sitemaps.views.index" title="永久链接至目标">¶</a></dt>
<dd></dd></dl>

<p>站点地图框架还能够创建一个站点地图索引，该索引能够引用单独的站点地图文件，每个部分在你的 <code class="docutils literal notranslate"><span class="pre">sitemaps</span></code> 字典中定义一个。唯一不同的用法是：</p>
<ul class="simple">
<li>你在 URLconf 中使用了两个视图： <a class="reference internal" href="#django.contrib.sitemaps.views.index" title="django.contrib.sitemaps.views.index"><code class="xref py py-func docutils literal notranslate"><span class="pre">django.contrib.sitemaps.views.index()</span></code></a> 和  <a class="reference internal" href="#django.contrib.sitemaps.views.sitemap" title="django.contrib.sitemaps.views.sitemap"><code class="xref py py-func docutils literal notranslate"><span class="pre">django.contrib.sitemaps.views.sitemap()</span></code></a>。</li>
<li><a class="reference internal" href="#django.contrib.sitemaps.views.sitemap" title="django.contrib.sitemaps.views.sitemap"><code class="xref py py-func docutils literal notranslate"><span class="pre">django.contrib.sitemaps.views.sitemap()</span></code></a> 视图应该采用 <code class="docutils literal notranslate"><span class="pre">section</span></code> 关键字作为参数。</li>
</ul>
<p>下面是上面例子的相关 URLconf 行的样子：</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">django.contrib.sitemaps</span> <span class="kn">import</span> <span class="n">views</span>

<span class="n">urlpatterns</span> <span class="o">=</span> <span class="p">[</span>
    <span class="n">path</span><span class="p">(</span><span class="s1">&#39;sitemap.xml&#39;</span><span class="p">,</span> <span class="n">views</span><span class="o">.</span><span class="n">index</span><span class="p">,</span> <span class="p">{</span><span class="s1">&#39;sitemaps&#39;</span><span class="p">:</span> <span class="n">sitemaps</span><span class="p">}),</span>
    <span class="n">path</span><span class="p">(</span><span class="s1">&#39;sitemap-&lt;section&gt;.xml&#39;</span><span class="p">,</span> <span class="n">views</span><span class="o">.</span><span class="n">sitemap</span><span class="p">,</span> <span class="p">{</span><span class="s1">&#39;sitemaps&#39;</span><span class="p">:</span> <span class="n">sitemaps</span><span class="p">},</span>
         <span class="n">name</span><span class="o">=</span><span class="s1">&#39;django.contrib.sitemaps.views.sitemap&#39;</span><span class="p">),</span>
<span class="p">]</span>
</pre></div>
</div>
<p>这将自动生成一个 <code class="file docutils literal notranslate"><span class="pre">sitemap.xml</span></code> 文件，同时引用 <code class="file docutils literal notranslate"><span class="pre">sitemap-flatpages.xml</span></code> 和 <code class="file docutils literal notranslate"><span class="pre">sitemap-blog.xml</span></code>。<a class="reference internal" href="#django.contrib.sitemaps.Sitemap" title="django.contrib.sitemaps.Sitemap"><code class="xref py py-class docutils literal notranslate"><span class="pre">Sitemap</span></code></a> 类和 <code class="docutils literal notranslate"><span class="pre">sitemaps</span></code> 字典完全没有变化。</p>
<p>如果你的一个网站地图有超过 50,000 个 URL，你应该创建一个索引文件。在这种情况下，Django 会自动对网站地图进行分页，索引也会反映出来。</p>
<p>如果你没有使用普通的站点地图视图——例如，如果它是用缓存装饰器包装的——你必须为你的站点地图视图命名，并将 <code class="docutils literal notranslate"><span class="pre">sitemap_url_name</span></code> 传给索引视图：</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">django.contrib.sitemaps</span> <span class="kn">import</span> <span class="n">views</span> <span class="k">as</span> <span class="n">sitemaps_views</span>
<span class="kn">from</span> <span class="nn">django.views.decorators.cache</span> <span class="kn">import</span> <span class="n">cache_page</span>

<span class="n">urlpatterns</span> <span class="o">=</span> <span class="p">[</span>
    <span class="n">path</span><span class="p">(</span><span class="s1">&#39;sitemap.xml&#39;</span><span class="p">,</span>
         <span class="n">cache_page</span><span class="p">(</span><span class="mi">86400</span><span class="p">)(</span><span class="n">sitemaps_views</span><span class="o">.</span><span class="n">index</span><span class="p">),</span>
         <span class="p">{</span><span class="s1">&#39;sitemaps&#39;</span><span class="p">:</span> <span class="n">sitemaps</span><span class="p">,</span> <span class="s1">&#39;sitemap_url_name&#39;</span><span class="p">:</span> <span class="s1">&#39;sitemaps&#39;</span><span class="p">}),</span>
    <span class="n">path</span><span class="p">(</span><span class="s1">&#39;sitemap-&lt;section&gt;.xml&#39;</span><span class="p">,</span>
         <span class="n">cache_page</span><span class="p">(</span><span class="mi">86400</span><span class="p">)(</span><span class="n">sitemaps_views</span><span class="o">.</span><span class="n">sitemap</span><span class="p">),</span>
         <span class="p">{</span><span class="s1">&#39;sitemaps&#39;</span><span class="p">:</span> <span class="n">sitemaps</span><span class="p">},</span> <span class="n">name</span><span class="o">=</span><span class="s1">&#39;sitemaps&#39;</span><span class="p">),</span>
<span class="p">]</span>
</pre></div>
</div>
</div>
<div class="section" id="s-template-customization">
<span id="template-customization"></span><h2>自定义模板<a class="headerlink" href="#template-customization" title="永久链接至标题">¶</a></h2>
<p>如果你希望为网站上的每个站点地图或站点地图索引使用不同的模板，你可以通过 URLconf 向 <code class="docutils literal notranslate"><span class="pre">sitemap</span></code> 和 <code class="docutils literal notranslate"><span class="pre">index</span></code> 视图传递一个 <code class="docutils literal notranslate"><span class="pre">template_name</span></code> 参数来指定它。</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">django.contrib.sitemaps</span> <span class="kn">import</span> <span class="n">views</span>

<span class="n">urlpatterns</span> <span class="o">=</span> <span class="p">[</span>
    <span class="n">path</span><span class="p">(</span><span class="s1">&#39;custom-sitemap.xml&#39;</span><span class="p">,</span> <span class="n">views</span><span class="o">.</span><span class="n">index</span><span class="p">,</span> <span class="p">{</span>
        <span class="s1">&#39;sitemaps&#39;</span><span class="p">:</span> <span class="n">sitemaps</span><span class="p">,</span>
        <span class="s1">&#39;template_name&#39;</span><span class="p">:</span> <span class="s1">&#39;custom_sitemap.html&#39;</span>
    <span class="p">}),</span>
    <span class="n">path</span><span class="p">(</span><span class="s1">&#39;custom-sitemap-&lt;section&gt;.xml&#39;</span><span class="p">,</span> <span class="n">views</span><span class="o">.</span><span class="n">sitemap</span><span class="p">,</span> <span class="p">{</span>
        <span class="s1">&#39;sitemaps&#39;</span><span class="p">:</span> <span class="n">sitemaps</span><span class="p">,</span>
        <span class="s1">&#39;template_name&#39;</span><span class="p">:</span> <span class="s1">&#39;custom_sitemap.html&#39;</span>
    <span class="p">},</span> <span class="n">name</span><span class="o">=</span><span class="s1">&#39;django.contrib.sitemaps.views.sitemap&#39;</span><span class="p">),</span>
<span class="p">]</span>
</pre></div>
</div>
<p>这些视图返回 <a class="reference internal" href="../template-response.html#django.template.response.TemplateResponse" title="django.template.response.TemplateResponse"><code class="xref py py-class docutils literal notranslate"><span class="pre">TemplateResponse</span></code></a> 实例，允许你在渲染前轻松定制响应数据。更多细节，请看 <a class="reference internal" href="../template-response.html"><span class="doc">TemplateResponse 文档</span></a>。</p>
<div class="section" id="s-context-variables">
<span id="context-variables"></span><h3>上下文变量<a class="headerlink" href="#context-variables" title="永久链接至标题">¶</a></h3>
<p>在自定义 <a class="reference internal" href="#django.contrib.sitemaps.views.index" title="django.contrib.sitemaps.views.index"><code class="xref py py-func docutils literal notranslate"><span class="pre">index()</span></code></a> 和 <a class="reference internal" href="#django.contrib.sitemaps.views.sitemap" title="django.contrib.sitemaps.views.sitemap"><code class="xref py py-func docutils literal notranslate"><span class="pre">sitemap()</span></code></a> 视图的模板时，可以依赖以下上下文变量。</p>
</div>
<div class="section" id="s-index">
<span id="index"></span><h3>索引<a class="headerlink" href="#index" title="永久链接至标题">¶</a></h3>
<p>变量 <code class="docutils literal notranslate"><span class="pre">sitemaps</span></code> 是指向每个站点地图的绝对 URL 的列表。</p>
</div>
<div class="section" id="s-id1">
<span id="id1"></span><h3>Sitemap<a class="headerlink" href="#id1" title="永久链接至标题">¶</a></h3>
<p>变量 <code class="docutils literal notranslate"><span class="pre">urlset</span></code> 是一个应该出现在站点地图中的 URL 列表，每个 URL 都会暴露出 <a class="reference internal" href="#django.contrib.sitemaps.Sitemap" title="django.contrib.sitemaps.Sitemap"><code class="xref py py-class docutils literal notranslate"><span class="pre">Sitemap</span></code></a> 类中定义的属性。</p>
<ul class="simple">
<li><code class="docutils literal notranslate"><span class="pre">alternates</span></code></li>
<li><code class="docutils literal notranslate"><span class="pre">changefreq</span></code></li>
<li><code class="docutils literal notranslate"><span class="pre">item</span></code></li>
<li><code class="docutils literal notranslate"><span class="pre">lastmod</span></code></li>
<li><code class="docutils literal notranslate"><span class="pre">location</span></code></li>
<li><code class="docutils literal notranslate"><span class="pre">priority</span></code></li>
</ul>
<p>当 <a class="reference internal" href="#django.contrib.sitemaps.Sitemap.i18n" title="django.contrib.sitemaps.Sitemap.i18n"><code class="xref py py-attr docutils literal notranslate"><span class="pre">i18n</span></code></a> 和 <a class="reference internal" href="#django.contrib.sitemaps.Sitemap.alternates" title="django.contrib.sitemaps.Sitemap.alternates"><code class="xref py py-attr docutils literal notranslate"><span class="pre">alternates</span></code></a> 被启用时，<code class="docutils literal notranslate"><span class="pre">alternates</span></code> 属性可用。它是一个其他语言版本的列表，包括可选的 <a class="reference internal" href="#django.contrib.sitemaps.Sitemap.x_default" title="django.contrib.sitemaps.Sitemap.x_default"><code class="xref py py-attr docutils literal notranslate"><span class="pre">x_default</span></code></a> 回退，对于每个 URL。每个候补是一个字典，有 <code class="docutils literal notranslate"><span class="pre">location</span></code> 和 <code class="docutils literal notranslate"><span class="pre">lang_code</span></code> 键。</p>
<div class="versionchanged">
<span class="title">Changed in Django 3.2:</span> <p>增加了 <code class="docutils literal notranslate"><span class="pre">alternates</span></code> 属性。</p>
</div>
<p>为每个 URL 添加了 <code class="docutils literal notranslate"><span class="pre">item</span></code> 属性，以便更灵活地定制模板，如 <a class="reference external" href="https://support.google.com/news/publisher-center/answer/9606710">Google news sitemaps</a> 。假设 Sitemap 的 <a class="reference internal" href="#django.contrib.sitemaps.Sitemap.items" title="django.contrib.sitemaps.Sitemap.items"><code class="xref py py-attr docutils literal notranslate"><span class="pre">items()</span></code></a> 会返回一个带有 <code class="docutils literal notranslate"><span class="pre">publication_data</span></code> 和 <code class="docutils literal notranslate"><span class="pre">tags</span></code> 字段的项目列表，类似这样就可以生成一个 Google News 兼容的网站地图。</p>
<div class="highlight-xml+django notranslate"><div class="highlight"><pre><span></span><span class="cp">&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;</span>
<span class="nt">&lt;urlset</span>
  <span class="na">xmlns=</span><span class="s">&quot;https://www.sitemaps.org/schemas/sitemap/0.9&quot;</span>
  <span class="na">xmlns:news=</span><span class="s">&quot;http://www.google.com/schemas/sitemap-news/0.9&quot;</span><span class="nt">&gt;</span>
<span class="cp">{%</span> <span class="k">spaceless</span> <span class="cp">%}</span>
<span class="cp">{%</span> <span class="k">for</span> <span class="nv">url</span> <span class="k">in</span> <span class="nv">urlset</span> <span class="cp">%}</span>
  <span class="nt">&lt;url&gt;</span>
    <span class="nt">&lt;loc&gt;</span><span class="cp">{{</span> <span class="nv">url.location</span> <span class="cp">}}</span><span class="nt">&lt;/loc&gt;</span>
    <span class="cp">{%</span> <span class="k">if</span> <span class="nv">url.lastmod</span> <span class="cp">%}</span><span class="nt">&lt;lastmod&gt;</span><span class="cp">{{</span> <span class="nv">url.lastmod</span><span class="o">|</span><span class="nf">date</span><span class="s2">:&quot;Y-m-d&quot;</span> <span class="cp">}}</span><span class="nt">&lt;/lastmod&gt;</span><span class="cp">{%</span> <span class="k">endif</span> <span class="cp">%}</span>
    <span class="cp">{%</span> <span class="k">if</span> <span class="nv">url.changefreq</span> <span class="cp">%}</span><span class="nt">&lt;changefreq&gt;</span><span class="cp">{{</span> <span class="nv">url.changefreq</span> <span class="cp">}}</span><span class="nt">&lt;/changefreq&gt;</span><span class="cp">{%</span> <span class="k">endif</span> <span class="cp">%}</span>
    <span class="cp">{%</span> <span class="k">if</span> <span class="nv">url.priority</span> <span class="cp">%}</span><span class="nt">&lt;priority&gt;</span><span class="cp">{{</span> <span class="nv">url.priority</span> <span class="cp">}}</span><span class="nt">&lt;/priority&gt;</span><span class="cp">{%</span> <span class="k">endif</span> <span class="cp">%}</span>
    <span class="nt">&lt;news:news&gt;</span>
      <span class="cp">{%</span> <span class="k">if</span> <span class="nv">url.item.publication_date</span> <span class="cp">%}</span><span class="nt">&lt;news:publication_date&gt;</span><span class="cp">{{</span> <span class="nv">url.item.publication_date</span><span class="o">|</span><span class="nf">date</span><span class="s2">:&quot;Y-m-d&quot;</span> <span class="cp">}}</span><span class="nt">&lt;/news:publication_date&gt;</span><span class="cp">{%</span> <span class="k">endif</span> <span class="cp">%}</span>
      <span class="cp">{%</span> <span class="k">if</span> <span class="nv">url.item.tags</span> <span class="cp">%}</span><span class="nt">&lt;news:keywords&gt;</span><span class="cp">{{</span> <span class="nv">url.item.tags</span> <span class="cp">}}</span><span class="nt">&lt;/news:keywords&gt;</span><span class="cp">{%</span> <span class="k">endif</span> <span class="cp">%}</span>
    <span class="nt">&lt;/news:news&gt;</span>
   <span class="nt">&lt;/url&gt;</span>
<span class="cp">{%</span> <span class="k">endfor</span> <span class="cp">%}</span>
<span class="cp">{%</span> <span class="k">endspaceless</span> <span class="cp">%}</span>
<span class="nt">&lt;/urlset&gt;</span>
</pre></div>
</div>
</div>
</div>
<div class="section" id="s-pinging-google">
<span id="pinging-google"></span><h2>通知谷歌<a class="headerlink" href="#pinging-google" title="永久链接至标题">¶</a></h2>
<p>当你的网站地图发生变化时，你可能想“ping”一下谷歌，让它知道要重新索引你的网站。站点地图框架提供了一个函数来实现这个功能： <a class="reference internal" href="#django.contrib.sitemaps.ping_google" title="django.contrib.sitemaps.ping_google"><code class="xref py py-func docutils literal notranslate"><span class="pre">django.contrib.sitemaps.ping_google()</span></code></a>。</p>
<dl class="function">
<dt id="django.contrib.sitemaps.ping_google">
<code class="descname">ping_google</code>(<em>sitemap_url=None</em>, <em>ping_url=PING_URL</em>, <em>sitemap_uses_https=True</em>)<a class="headerlink" href="#django.contrib.sitemaps.ping_google" title="永久链接至目标">¶</a></dt>
<dd><p><code class="docutils literal notranslate"><span class="pre">ping_google</span></code> 接受这些可选的参数：</p>
<ul class="simple">
<li><code class="docutils literal notranslate"><span class="pre">sitemap_url</span></code> - 通往网站站点地图的绝对路径（例如： <code class="file docutils literal notranslate"><span class="pre">'/sitemap.xml'</span></code>）。如果没有提供这个参数，<code class="docutils literal notranslate"><span class="pre">ping_google</span></code> 将尝试通过在 URLconf 中进行反向查找来获取网站地图。</li>
<li><code class="docutils literal notranslate"><span class="pre">ping_url</span></code> - 默认为谷歌的 Ping 工具：<a class="reference external" href="https://www.google.com/webmasters/tools/ping">https://www.google.com/webmasters/tools/ping</a>。</li>
<li><code class="docutils literal notranslate"><span class="pre">sitemap_uses_https</span></code> - 如果你的网站使用 <code class="docutils literal notranslate"><span class="pre">http</span></code> 而不是 <code class="docutils literal notranslate"><span class="pre">https</span></code>，设置为 <code class="docutils literal notranslate"><span class="pre">False</span></code>。</li>
</ul>
<p><a class="reference internal" href="#django.contrib.sitemaps.ping_google" title="django.contrib.sitemaps.ping_google"><code class="xref py py-func docutils literal notranslate"><span class="pre">ping_google()</span></code></a> 如果不能确定你的站点地图 URL，会引发异常 <code class="docutils literal notranslate"><span class="pre">django.contrib.sitemaps.SitemapNotFound</span></code>。</p>
</dd></dl>

<div class="admonition-register-with-google-first admonition">
<p class="first admonition-title">首先向谷歌注册！</p>
<p class="last">The <a class="reference internal" href="#django.contrib.sitemaps.ping_google" title="django.contrib.sitemaps.ping_google"><code class="xref py py-func docutils literal notranslate"><span class="pre">ping_google()</span></code></a> command only works if you have registered your
site with <a class="reference external" href="https://search.google.com/search-console/welcome">Google Search Console</a>.</p>
</div>
<p>调用 <a class="reference internal" href="#django.contrib.sitemaps.ping_google" title="django.contrib.sitemaps.ping_google"><code class="xref py py-func docutils literal notranslate"><span class="pre">ping_google()</span></code></a> 的一个有用的方法是在模型的 <code class="docutils literal notranslate"><span class="pre">save()</span></code> 方法：</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">django.contrib.sitemaps</span> <span class="kn">import</span> <span class="n">ping_google</span>

<span class="k">class</span> <span class="nc">Entry</span><span class="p">(</span><span class="n">models</span><span class="o">.</span><span class="n">Model</span><span class="p">):</span>
    <span class="c1"># ...</span>
    <span class="k">def</span> <span class="nf">save</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">force_insert</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span> <span class="n">force_update</span><span class="o">=</span><span class="kc">False</span><span class="p">):</span>
        <span class="nb">super</span><span class="p">()</span><span class="o">.</span><span class="n">save</span><span class="p">(</span><span class="n">force_insert</span><span class="p">,</span> <span class="n">force_update</span><span class="p">)</span>
        <span class="k">try</span><span class="p">:</span>
            <span class="n">ping_google</span><span class="p">()</span>
        <span class="k">except</span> <span class="ne">Exception</span><span class="p">:</span>
            <span class="c1"># Bare &#39;except&#39; because we could get a variety</span>
            <span class="c1"># of HTTP-related exceptions.</span>
            <span class="k">pass</span>
</pre></div>
</div>
<p>然而，一个更有效的解决方案是在定时脚本中调用 <a class="reference internal" href="#django.contrib.sitemaps.ping_google" title="django.contrib.sitemaps.ping_google"><code class="xref py py-func docutils literal notranslate"><span class="pre">ping_google()</span></code></a>，或者其他一些预定任务。该函数向谷歌的服务器发出 HTTP 请求，所以你可能不想在每次调用 <code class="docutils literal notranslate"><span class="pre">save()</span></code> 时引入网络开销。</p>
<div class="section" id="s-pinging-google-via-manage-py">
<span id="pinging-google-via-manage-py"></span><h3>通过 <code class="docutils literal notranslate"><span class="pre">manage.py</span></code> 通知谷歌<a class="headerlink" href="#pinging-google-via-manage-py" title="永久链接至标题">¶</a></h3>
<dl class="django-admin">
<dt id="django-admin-ping_google">
<code class="descname">django-admin ping_google [sitemap_url]</code><a class="headerlink" href="#django-admin-ping_google" title="永久链接至目标">¶</a></dt>
<dd></dd></dl>

<p>一旦将站点地图应用程序添加到你的项目中，你也可以使用 <code class="docutils literal notranslate"><span class="pre">ping_google</span></code> 管理命令通知谷歌：</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">python</span> <span class="n">manage</span><span class="o">.</span><span class="n">py</span> <span class="n">ping_google</span> <span class="p">[</span><span class="o">/</span><span class="n">sitemap</span><span class="o">.</span><span class="n">xml</span><span class="p">]</span>
</pre></div>
</div>
<dl class="django-admin-option">
<dt id="cmdoption-ping-google-sitemap-uses-http">
<code class="descname">--sitemap-uses-http</code><code class="descclassname"></code><a class="headerlink" href="#cmdoption-ping-google-sitemap-uses-http" title="永久链接至目标">¶</a></dt>
<dd></dd></dl>

<p>如果你的网站地图使用 <code class="docutils literal notranslate"><span class="pre">http</span></code> 而不是 <code class="docutils literal notranslate"><span class="pre">https</span></code>，请使用这个选项。</p>
</div>
</div>
</div>


          </div>
        </div>
      </div>
      
        
          <div class="yui-b" id="sidebar">
            
      <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
        <div class="sphinxsidebarwrapper">
  <h3><a href="../../contents.html">Table of Contents</a></h3>
  <ul>
<li><a class="reference internal" href="#">站点地图框架</a><ul>
<li><a class="reference internal" href="#overview">概况</a></li>
<li><a class="reference internal" href="#installation">安装</a></li>
<li><a class="reference internal" href="#initialization">初始化</a></li>
<li><a class="reference internal" href="#sitemap-classes"><code class="docutils literal notranslate"><span class="pre">Sitemap</span></code> 类</a></li>
<li><a class="reference internal" href="#an-example">一个例子</a></li>
<li><a class="reference internal" href="#sitemap-class-reference"><code class="docutils literal notranslate"><span class="pre">Sitemap</span></code> 类参考</a></li>
<li><a class="reference internal" href="#shortcuts">捷径</a><ul>
<li><a class="reference internal" href="#example">例如</a></li>
</ul>
</li>
<li><a class="reference internal" href="#sitemap-for-static-views">静态视图的站点地图</a></li>
<li><a class="reference internal" href="#creating-a-sitemap-index">创建站点地图索引</a></li>
<li><a class="reference internal" href="#template-customization">自定义模板</a><ul>
<li><a class="reference internal" href="#context-variables">上下文变量</a></li>
<li><a class="reference internal" href="#index">索引</a></li>
<li><a class="reference internal" href="#id1">Sitemap</a></li>
</ul>
</li>
<li><a class="reference internal" href="#pinging-google">通知谷歌</a><ul>
<li><a class="reference internal" href="#pinging-google-via-manage-py">通过 <code class="docutils literal notranslate"><span class="pre">manage.py</span></code> 通知谷歌</a></li>
</ul>
</li>
</ul>
</li>
</ul>

  <h4>上一个主题</h4>
  <p class="topless"><a href="redirects.html"
                        title="上一章">重定向应用</a></p>
  <h4>下一个主题</h4>
  <p class="topless"><a href="sites.html"
                        title="下一章">“站点”框架</a></p>
  <div role="note" aria-label="source link">
    <h3>本页</h3>
    <ul class="this-page-menu">
      <li><a href="../../_sources/ref/contrib/sitemaps.txt"
            rel="nofollow">显示源代码</a></li>
    </ul>
   </div>
<div id="searchbox" style="display: none" role="search">
  <h3>快速搜索</h3>
    <div class="searchformwrapper">
    <form class="search" action="../../search.html" method="get">
      <input type="text" name="q" />
      <input type="submit" value="转向" />
      <input type="hidden" name="check_keywords" value="yes" />
      <input type="hidden" name="area" value="default" />
    </form>
    </div>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
        </div>
      </div>
              <h3>Last update:</h3>
              <p class="topless">7月 23, 2021</p>
          </div>
        
      
    </div>

    <div id="ft">
      <div class="nav">
    &laquo; <a href="redirects.html" title="重定向应用">previous</a>
     |
    <a href="../index.html" title="API 参考" accesskey="U">up</a>
   |
    <a href="sites.html" title="“站点”框架">next</a> &raquo;</div>
    </div>
  </div>

      <div class="clearer"></div>
    </div>
  </body>
</html>